import { Component, OnInit } from '@angular/core';
// 引入该组件需使用的服务
import { MyserviceService } from '../../services/myservice.service'


@Component({
  selector: 'app-todolist',
  templateUrl: './todolist.component.html',
  styleUrls: ['./todolist.component.css']
})
export class TodolistComponent implements OnInit {
  public doList:any[] = []
  public doneList:any[] = []
  public newToDo:string =''

  // 初始化服务
  // 依赖注入？？？
  constructor(public myserve:MyserviceService) { 
    
  }

  ngOnInit(): void {
    // 使用服务中的方法
    this.myserve.show()
    console.log(this.myserve.animalInfor)
  }
  enterBtn(e:any):void{
    e.keyCode == 13?this.handleAdd():''
  }
  handleAdd():void{
    let isReapet = this.doList.some((item) => {
      return item.text == this.newToDo
    })
    if(!isReapet && this.newToDo.trim() != ''){
      this.doList.push({
        text:this.newToDo,
        state:false
      })
      this.newToDo = ''
    }
  }
  changState(text:any,self:any[],target:any[]){
    self.forEach((item,index) => {
      if(item.text == text){
        item.state = !item.state
        target.push(item)
        self.splice(index,1)
      }
    })
  }
  changeStateDone(text:any):void{
    this.changState(text,this.doList,this.doneList)
  }
  changeStateNotDo(text:any){
    this.changState(text,this.doneList,this.doList)
  }
  delData(index:any,state:boolean):void{
    // 通过下标删除
    state?this.doneList.splice(index,1):this.doList.splice(index,1)
    // 通过遍历查询删除
    // if(state){
    //   this.doneList.forEach((item,index) => {
    //     item.text == text?this.doneList.splice(index,1):''
    //   })
    // }else{
    //   this.doList.forEach((item,index) => {
    //     item.text == text?this.doList.splice(index,1):''
    //   })
    // }
  }
}
